package org.jeecg.modules.demo3.peijian.utill;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author wjn
 * @version V1.0
 * @date 2021/7/29 15:19
 * @Copyright © 2021/7/29 深圳卓翼航空无人机有限公司
 */
/*pdf 文件相互转换工具类*/
public class PdfUtill {


    /*获取上传文件路径*/
    public static String getPath(String id){
        MultipartFile file = GetFile.multipartFile;
        String realPath = "C:\\PDF\\fly\\axhk\\";
        String newName = file.getOriginalFilename();
        String path = realPath+id+newName;
        return path;
    }

    /*公共方法 删除pdf文件*/
    public static void pub(String id){
        MultipartFile file = GetFile.multipartFile;
        String realPath = "C:\\PDF\\fly\\axhk\\";
        String newName = file.getOriginalFilename();
        String path = realPath+id+newName;
        DeleteFile deleteFiles = new DeleteFile();
        deleteFiles.deleteFile(path);
    }

    /*pdf文件拷贝到规定文件*/
    public static void copyFile(String newPath,String id){
        String oldPath = getPath(id);
        File oldfile = new File(oldPath);  //上传的pdf文件
        File newfile = new File(newPath); //生成新的储存路劲
        try {
            if(!newfile.exists()){ //新目录不存在创建
                newfile.mkdirs();
            }
            FileUtils.copyFileToDirectory(oldfile, newfile);//复制完后
            pub(id); //删除原来的文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*返回文件名*/
    public static String getPdfName(){
        MultipartFile file = GetFile.multipartFile;
        String newName = file.getOriginalFilename();
        return newName;
    }


    /*
    * 文件重命名
    * path 原文件路径
    * oldName 原文件名
    * newName 新文件名
    * */
    public static boolean getByfileName(String path,String oldName,String newName){

        File ofile = new File(path,oldName); //旧文件
        File nfile = new File(path,newName); //新文件

        if(!ofile.exists()){ //查看源文件是否存在 如果不存在直接不用创建
            System.out.println("原文件不存在！");
            return false;
        }
        if(nfile.exists()){  //查看新文件 是否存在 如果存在不用创建
            System.out.println("改文件已经存在！");
            return false;
        }else {//如果 不满足以上条件 则重新命名
            ofile.renameTo(nfile);
            return true;
        }
    }

    /*
    * 文件下载
    * String path
    * */
   /* public static void downFile(String fileName, HttpServletResponse response){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File file = new File(fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开            
                response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
                inputStream = new BufferedInputStream(new FileInputStream(file));
                outputStream = response.getOutputStream();
                byte[] buf = new byte[inputStream.available()];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
                response.flushBuffer();
            }
        } catch (Exception e) {
            System.out.println("文件下载失败" + e.getMessage());
            // e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/


}
